package zxq.rollcall.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import zxq.rollcall.course.controller.action.ICourseController;
import zxq.rollcall.course.entity.SystemClassTableEntity;
import zxq.rollcall.course.entity.SystemCourseTableEntity;
import zxq.rollcall.course.entity.dto.ResultDTO;
import zxq.rollcall.course.entity.dto.db.CourseSignDTO;
import zxq.rollcall.course.entity.dto.db.StudentInfoDTO;
import zxq.rollcall.course.entity.dto.db.StudentSignDTO;
import zxq.rollcall.course.entity.dto.db.TeacherSignDTO;
import zxq.rollcall.course.entity.dto.set.CourseDTO;
import zxq.rollcall.course.entity.query.CourseQuery;
import zxq.rollcall.course.entity.query.PageQuery;
import zxq.rollcall.course.service.MPSystemCourseTableService;
import zxq.rollcall.course.service.MPSystemSignTableService;
import zxq.rollcall.course.service.MPSystemTeacherSignTableService;
import zxq.rollcall.course.service.MPSystemUserTableService;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2020-01-22
 */
@Controller
@RequestMapping("/course/api/curriculum")
@Api(tags = "系统课程表")
@Slf4j
public class SystemCourseTableAction implements ICourseController {


    /**
     * 用户签到
     */
    ConcurrentHashMap<String,Integer> signHash = new ConcurrentHashMap();


    /**
     * 课程服务
      */
    @Autowired
    MPSystemCourseTableService courseTableService;
    /**
     * 用户服务
      */
    @Autowired
    MPSystemUserTableService userTableService;

    /**
     * 老师签到服务
     */
    @Autowired
    MPSystemTeacherSignTableService teacherSignTableService;

    /**
     * 记录签到的服务
     */
    @Autowired
    MPSystemSignTableService mpSystemSignTableService;



    @Override
    public ResultDTO<Boolean> save(CourseQuery courseQuery) {
        SystemCourseTableEntity courseTableEntity = new SystemCourseTableEntity();
        BeanUtils.copyProperties(courseQuery,courseTableEntity);
        Boolean rst = courseTableService.save(courseTableEntity);
        if(rst) {
            return  new ResultDTO<>(1,"插入成功",true);
        }
        return new ResultDTO<>(0,"插入失败",false);
    }

    @Override
    public ResultDTO<Boolean> update(Integer id, CourseQuery courseQuery) {
        SystemCourseTableEntity courseTableEntity = new SystemCourseTableEntity();
        BeanUtils.copyProperties(courseQuery,courseTableEntity);
        courseTableEntity.setId(id);
        Boolean rst = courseTableService.updateById(courseTableEntity);
        if(rst) {
            return  new ResultDTO<>(1,"更新成功",true);
        }
        return new ResultDTO<>(0,"更新失败",false);
    }

    @Override
    public ResultDTO<Boolean> delete(Integer id) {
        Boolean rst = courseTableService.removeById(id);
        if(rst) {
            return  new ResultDTO<>(1,"删除成功",true);
        }
        return new ResultDTO<>(0,"删除失败",false);
    }

    @Override
    public ResultDTO<List<CourseDTO>> query(PageQuery pageQuery) {
        Page page = new Page<CourseDTO>(pageQuery.getPage(),pageQuery.getCount());
        List<CourseDTO> courseDTOS = courseTableService.page(page).getRecords();
        return new ResultDTO<>(1,"查询成功",courseDTOS);
    }

    @Override
    public ResultDTO<List<CourseDTO>> studentQuery(Integer stuId,Boolean isAll) {
         Integer classId = userTableService.queryClassId(stuId);
         if(classId == null) {
             return  new ResultDTO<>(0,"学生班级id为空",null);
         }
         SystemCourseTableEntity courseTableEntity = new SystemCourseTableEntity();
         courseTableEntity.setClassesId(classId);
         if(!isAll) {
             courseTableEntity.setWhatDay(courseTableService.getDayOfWeek());
         }
        QueryWrapper<SystemCourseTableEntity> queryWrapper = new QueryWrapper<>(courseTableEntity);
        queryWrapper.orderByAsc("what_day");
         List<CourseDTO> rst =  courseTableService.list(queryWrapper).stream()
                 .map(item -> {
                     CourseDTO courseDTO = new CourseDTO();
                     BeanUtils.copyProperties(item,courseDTO);
                     return courseDTO;
                 }).collect(Collectors.toList());
        return new ResultDTO<>(1,"查询成功",rst);
    }

    @Override
    public ResultDTO<List<CourseDTO>> teacherQuery(Integer teacherId,Boolean isAll) {
        SystemCourseTableEntity courseTableEntity = new SystemCourseTableEntity();
        courseTableEntity.setTeacherId(teacherId);
        if(!isAll) {
            courseTableEntity.setWhatDay(courseTableService.getDayOfWeek());
        }
        QueryWrapper<SystemCourseTableEntity> queryWrapper = new QueryWrapper<>(courseTableEntity);
        queryWrapper.orderByAsc("what_day");
        List<CourseDTO> rst = courseTableService.list(queryWrapper).stream()
                .map(item -> {
                    CourseDTO courseDTO = new CourseDTO();
                    BeanUtils.copyProperties(item,courseDTO);
                    return courseDTO;
                }).collect(Collectors.toList());
        return new ResultDTO<>(1,"查询成功",rst);
    }



    @Override
    public ResultDTO<CourseDTO> courseQuery(Integer courseId) {
        SystemCourseTableEntity courseTableEntity = courseTableService.getById(courseId);
        if(courseTableEntity != null) {
            CourseDTO courseDTO = new CourseDTO();
            BeanUtils.copyProperties(courseTableEntity, courseDTO);
            return new ResultDTO<>(1,"查询成功",courseDTO);
        }
        return new ResultDTO<>(0,"查询失败",null);
    }

    @Override
    public ResultDTO<List<CourseSignDTO>> courseSignQuery(Integer courseId) {
        List<CourseSignDTO> courseSignDTOS = mpSystemSignTableService.queryCourseSign(courseId);
        return new ResultDTO<>(1,"查询成功",courseSignDTOS);
    }


    @Override
    public ResultDTO<Boolean> teacherSign(Integer courseId, Boolean sign) {
        log.info("courseId:" + courseId +" startSign:" + sign);
        if(sign) {
           if(!courseTableService.startSign(courseId)){
               return new ResultDTO<>(0,"开始签到失败",false);
           }
           Integer teacherSignId = teacherSignTableService.createTeacherSign(courseId);
           Integer classId = courseTableService.getClassId(courseId);
           List<Integer> stuIds = userTableService.queryStudent(classId).stream().map(
                   item -> item.getId()
           ).collect(Collectors.toList());
           Boolean rst = mpSystemSignTableService.initStudentSign(teacherSignId,stuIds);
           if(!rst) {
               return new ResultDTO<>(0,"开始签到失败",false);
           }
           signHash.put(courseId.toString() ,teacherSignId);
        } else {
            if(!courseTableService.stopSign(courseId)) {
              return new ResultDTO<>(0,"结束签到失败",false);
            }
            signHash.remove(courseId.toString());
        }

        return new ResultDTO<>(1,"操作成功",true);
    }

    @Override
    public ResultDTO<Boolean> studentSign(Integer courseId, Integer studentId) {
         Integer teacherId = signHash.get(courseId.toString());
         if(teacherId == null) {
             return new ResultDTO<>(0,"签到已结束",false);
         }
         Boolean rst = mpSystemSignTableService.updateStudentSign(teacherId,studentId);
         if(!rst) {
             return new ResultDTO<>(0,"签到失败",false);
         }
         return new ResultDTO<>(0,"签到成功",true);
    }

    @Override
    public ResultDTO<List<CourseSignDTO>> queryCourseSignDetail(Integer courseId) {
        List<CourseSignDTO> courseSignDTOS = mpSystemSignTableService.queryCourseSign(courseId);
        return new ResultDTO<>(1,"查询成功",courseSignDTOS);
    }


    @Override
    public ResultDTO<CourseSignDTO> queryNewCourse(Integer courseId) {
        CourseSignDTO courseSignDTO =  mpSystemSignTableService.queryCourseTeacherSign(courseId,teacherSignTableService.queryNewTeacherId(courseId));
        return new ResultDTO<>(1,"查询成功",courseSignDTO);
    }

    @Override
    public ResultDTO<List<StudentSignDTO>> queryGatherStudentStatistics(Integer courseId) {
        List<StudentSignDTO> signDTOS =  mpSystemSignTableService.queryStudentSign(courseId);
        return new ResultDTO<>(1,"查询成功",signDTOS);
    }

    @Override
    public ResultDTO<StudentSignDTO> querySingleStudentStatistics(Integer courseId, Integer studentId) {
        StudentSignDTO studentSignDTO = mpSystemSignTableService.queryStudentSignDetail(courseId,studentId);
        return new ResultDTO<>(1,"查询成功",studentSignDTO);
    }

    @Override
    public ResultDTO<List<StudentInfoDTO>> queryStudentSignDetail(Integer studentId,PageQuery pageQuery) {
        List<StudentInfoDTO> studentInfoDTOS = mpSystemSignTableService.queryStudentInfoSign(studentId,pageQuery);
        return new ResultDTO<>(1,"查询成功",studentInfoDTOS);
    }

    @Override
    public ResultDTO<List<StudentInfoDTO>> queryStudentCourseSignDetail(Integer studentId, Integer courseId, PageQuery pageQuery) {
        List<StudentInfoDTO> data = mpSystemSignTableService.queryCourseStudentInfoSign(studentId,courseId,pageQuery);
        return new ResultDTO<>(1,"查询成功",data);
    }

    @Override
    public ResultDTO<List<TeacherSignDTO>> queryTeacherSignDetail(Integer teacherSignId) {
        List<TeacherSignDTO> teacherSignDTOS = mpSystemSignTableService.queryTeacherSignDetail(teacherSignId);
        return new ResultDTO<>(1,"查询成功",teacherSignDTOS);
    }


}

